using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UMC.Data;
using UMC.WorkFlow.Subassembly;

namespace UMC.WorkFlow
{
    /// <summary>
    /// 工作流中的基本身份
    /// </summary>

    public class Identity : IJSON
    {
        public Identity()
        {
            this.Username = String.Empty;
        }
        public string Alias
        {
            get; set;
        }
        public Identity(UMC.Security.AccessToken token)
        {
            this.Username = token.Username;
            this.Alias = token.Alias;
        }
        public Identity(string username, String alias)
        {
            this.Username = username;
            this.Alias = alias;
        }
        /// <summary>
        /// 用户名
        /// </summary>
        [Design(Caption = "用户名")]
        public string Username { get; set; }

        /// <summary>
        /// 代理
        /// </summary>
        /// <param name="trustor">委托人</param>
        /// <param name="taskName">作业名</param>
        /// <returns></returns>
        public virtual bool Proxy(Identity trustor, ulong taskId)
        {
            return false;
            //    return UMC.WorkFlow.TaskProxyer.Instance().TaskProxy(trustor.Username, this.Username, taskId);
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            {
                case "Alias":
                    this.Alias = value as string;
                    break;
                case "Username":
                    this.Username = value as string;
                    break;
                    //,LineStatus.UnPass);
            }
        }
        protected virtual void WriteProperty(TextWriter writer)
        {
            if (String.IsNullOrEmpty(this.Alias) == false)
            {
                writer.Write(',');
                JSON.Serialize("Alias", this.Alias, writer);
            }
            if (String.IsNullOrEmpty(this.Username) == false)
            {
                writer.Write(',');
                JSON.Serialize("Username", this.Username, writer);
            }
        }

        protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public void Write(TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, writer);
            writer.Write(":");
            JSON.Serialize(JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');
        }

    }
}
